﻿<%@ WebHandler Language="C#" Class="FileReceive" %>

using System;
using System.Web;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class FileReceive : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        MyLog mylog = new MyLog(context);

        context.Response.ContentType = "text/plain";

        if (context.Request.Files.Count > 0)
        {
            try
            {
                HttpPostedFile file = context.Request.Files[0];

                // like "test.txt" , fileName is contain the extension
                string fileName = file.FileName;

                string fileExtension = System.IO.Path.GetExtension(fileName);

                JToken conf = (JToken)JsonConvert.DeserializeObject(File.ReadAllText(context.Server.MapPath("../Conf/FileReceiveConf.json")));
                string receivePackZipPath = conf["receivePackZipPath"].ToString();
                string saveLocation = context.Server.MapPath("~/" + receivePackZipPath) + "\\" + DateTime.Today.ToString("yyyy-MM-dd");
                if (Directory.Exists(saveLocation) == false)
                {
                    mylog.writeLog("saving path : " + saveLocation + " not exists, now create");
                    Directory.CreateDirectory(saveLocation);
                }

                file.SaveAs(saveLocation + "\\" + fileName);

                mylog.writeLog("receiving and saving file : " + fileName + " succeed");

                context.Response.Write("文件接收成功");

                mylog.burn(); //日志老化

                FileHelper fileHelp = new FileHelper(context);
                fileHelp.burnReceiveDirectory(); //老化zip
            }
            catch (Exception ex)
            {
                context.Response.Write("文件接收失败:" + ex.Message);
            }
        }
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}